<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="性别" prop="sex">
        <el-input
          v-model="queryParams.sex"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="学号" prop="studentID">
        <el-input
          v-model="queryParams.studentID"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="入学年份" prop="enterSchoolYear">
        <el-input
          v-model="queryParams.enterSchoolYear"
          placeholder="请输入入学年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="是否启用" prop="disabled">
        <el-input
          v-model="queryParams.disabled"
          placeholder="请输入是否启用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>






      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>


    </el-form>









    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wxuser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:wxuser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:wxuser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wxuser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>





    <el-table v-loading="loading" :data="wxuserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="用户id" align="center" prop="id" /> -->

      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="学号" align="center" prop="studentID" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="手机号" align="center" prop="telephone" />
      <el-table-column label="入学年份" align="center" prop="enterSchoolYear" />
      <el-table-column label="学籍状态" align="center" prop="schoolStatu" />
      <el-table-column label="是否启用" align="center" prop="disabled" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:wxuser:edit']"
          >修改</el-button>

          <!-- v-hasPermi="['system:wxuser:remove']" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>





<!--
.sync 语法糖
 子组件定义事件，给父组件使用。。。
-->

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />







    <!-- 添加或修改微信用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">


        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="学号" prop="studentID">
          <el-input v-model="form.studentID" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入学年份" prop="enterSchoolYear">
          <el-input v-model="form.enterSchoolYear" placeholder="请输入入学日期" />
        </el-form-item>
        <el-form-item label="是否启用" prop="disabled">
          <el-input v-model="form.disabled" placeholder="请输入是否启用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>






<script>
import { listWxuser, getWxuser, delWxuser, addWxuser, updateWxuser } from "@/api/appManager/wxuser/wxuser.js";




export default {
  name: "Wxuser",



  data() {
    return {
      // 遮罩层
      loading: true,
      imageRef:"",


      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,


      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,


      // 表单参数  这个是修改弹框的表单
      form: {},



      // 表单校验
      rules: {
        openid: [
          { required: true, message: "openid不能为空", trigger: "blur" }
        ],
      },


      // 这里是负责查询和列表查询的参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        studentID: null,
        disabled: null,
        enterSchoolYear:null
      },

      // 总条数
      total: 0,
      // 微信用户表格数据
      wxuserList: [],




    };
  },
  created() {
    this.getList();
  },




  methods: {


    /** 查询微信用户列表 */
    getList() {
      this.loading = true;
      listWxuser(this.queryParams).then(response => {
        this.wxuserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },









    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        studentID: null,
        enterSchoolYear: null,
        degreeType:null,
        schoolStatu: null,
        disabled: null
      };

      // 这个只能清除表单里参数，而上边这个可以清除所有数据，主要是为了清除id，这样点击确定的时候，才不会出现因为没删除id，结果把新增当成修改
      this.resetForm("form");
    },




    /** 重置按钮操作 */
    resetQuery() {
      // 这个方法就是专门用来清除表单里的数据的
      this.resetForm("queryForm");
      this.handleQuery();
    },







    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },




    // 被选中时触发
    handleSelectionChange(selection) {
      // selection  存的是被选中的对象，为一个数组
      // 数组的map方法，遍历，reutrn每一个值，最后组成一个新数组
      this.ids = selection.map(item => item.id)
      //不单一为true  单一为flase
      // 这个要给disable用，flase的意思为 为单个，所以可以不 不显示，就是可以显示
      this.single = selection.length!==1
      // 为空时为ture
      this.multiple = !selection.length


    },




    /** 新增按钮操作 */
    handleAdd() {
      console.log(this.form);
      this.reset();
      this.open = true;
      this.title = "添加微信用户";
    },




    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // 用的按钮 ids有值 row为空 用ids， 使用表格修改，row.id 逻辑或，直接用row.id   实现了点击修改按钮用ids， 点击表格修改用id
      const id = row.id || this.ids
      getWxuser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信用户";
      });
    },






    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWxuser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWxuser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },





    /** 删除按钮操作 */
    handleDelete(row) {

// 这个是点睛之笔，点击删除按钮是row.id为空，  点击表格中的删除时 多选为空，用ids为空用id，或者 ids不为空两个都为true  逻辑或用前边的，也是用id
      const ids = row.id || this.ids;
      console.log(ids);

      this.$modal.confirm('是否确认删除微信用户编号为"' + ids + '"的数据项？').then(function() {
        return delWxuser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },





    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wxuser/export', {
        ...this.queryParams
      }, `wxuser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
